package com.wd.health.wxapi;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.wd.health.model.bean.WxLoginBean;
import com.wd.health.utils.RetrofitManager;
import com.wd.health.utils.RsaCoder;
import com.wd.health.utils.Utils;
import com.wd.health.view.activity.mine.LoginActivity;
import com.wd.health.view.activity.mine.PerfectMailActivity;

import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.api.BasicCallback;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * @ProjectName:shopmall
 * @Package:com.wd.health.wxapi
 * @ClassName:WXEntryActivity
 * @UpdateUser: 裴童童
 * @date: 2021/1/3
 */
public class WXEntryActivity extends AppCompatActivity implements IWXAPIEventHandler {
    private IWXAPI api;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api= WXAPIFactory.createWXAPI(this,"wxe3fcbe8a55cd33ff",true);
        api.handleIntent(getIntent(), this);
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp baseResp) {
        switch (baseResp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                Log.e("TAG", "onResp OK");
                if (baseResp instanceof SendAuth.Resp) {
                    SendAuth.Resp newResp = (SendAuth.Resp) baseResp;
                    //获取微信传回的code
                    String code = newResp.code;
                    Log.e("TAG", "onResp: "+code);
                    //获取到code值然后进行跳转
                    RetrofitManager.getInstance().postWxLoginDate(code, new Observer<WxLoginBean>() {
                        @Override
                        public void onSubscribe(Disposable d) {

                        }

                        @Override
                        public void onNext(WxLoginBean wxLoginBean) {
                            Toast.makeText(WXEntryActivity.this, "微信登录成功", Toast.LENGTH_SHORT).show();
                            String headPic = wxLoginBean.getResult().getHeadPic();
                            int userId = wxLoginBean.getResult().getUserId();
                            String sessionId = wxLoginBean.getResult().getSessionId();
                            String userName = wxLoginBean.getResult().getUserName();
                            String nickName = wxLoginBean.getResult().getNickName();
                            String jiGuangPwd = wxLoginBean.getResult().getJiGuangPwd();
                            SharedPreferences user = getSharedPreferences("user", Context.MODE_PRIVATE);
                            user.edit()
                                    .putString("headPic",headPic)
                                    .putString("sessionId",sessionId)
                                    .putString("userName",userName)
                                    .putString("nickName",nickName)
                                    .putInt("userId",userId)
                                    .putBoolean("login",true)
                                    .commit();
                            try {
                                String jiguangpwd = RsaCoder.decryptByPublicKey(jiGuangPwd);
                                String s = Utils.md5(jiguangpwd);
                                JMessageClient.login(userName,s, new BasicCallback() {
                                    @Override
                                    public void gotResult(int i, String s) {
                                        if (i == 0){
                                            Log.i("TAG", "gotResult:"+"极光登录成功");
                                        }
                                    }
                                });
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            String email = wxLoginBean.getResult().getEmail();
                            if (email.equals("")){
                                Intent intent = new Intent(WXEntryActivity.this, PerfectMailActivity.class);
                                startActivity(intent);
                                finish();
                            }
                        }

                        @Override
                        public void onError(Throwable e) {

                        }

                        @Override
                        public void onComplete() {

                        }
                    });
                }
                finish();
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                Log.i("WXTest", "onResp ERR_USER_CANCEL ");
                //发送取消
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                Log.i("WXTest", "onResp ERR_AUTH_DENIED");
                //发送被拒绝
                break;
            default:
                Log.i("WXTest", "onResp default errCode " + baseResp.errCode);
                //发送返回
                break;
        }
        finish();
    }
}
